<?php

namespace App\Http\Controllers\Admin;

use App\Models\Area;
use App\Models\Category;
use App\Models\Goods;

class GoodsController extends BaseController
{
    public function __construct(Goods $goods, Category $category, Area $area)
    {
        parent::__construct();
        $this->goods = $goods;
        $this->category = $category;
        $this->area = $area;
    }

    public function getIndex()
    {

        $request = request();
        $request['pid'] = 258;

        $flag = 'goods';
        $categories = $this->area->getChild($request);

        $rows = $this->goods->getGoodsList(request());

        //dd($rows);
        return view('agent.goods.index',compact('rows','flag'));
    }

    public function getCategory()
    {
        $categories = $this->category->getChild(request());
        return $categories;
    }

    public function getAddView()
    {
        $flag = 'goods';
        $categories = $this->getCategory();
        return view('agent.goods.add_goods_step1',compact('categories','flag'));
    }

    public function getAddViewStep(){
        $flag = 'goods';
        return view('agent.goods.add_goods_step2',compact('flag'));
    }


    //添加商品
    public function postAdd()
    {
        $rows = $this->goods->addGoods(request());
        return ajax_return($rows);
    }

//    public function getEdit()
//    {
//        $this->member->logout();
//        return redirect(action('Home\MemberController@getLogin'));
//    }

//    public function postEdit()
//    {
//         $res = $this->goods->getGoodsInfoById(request('id'));
//         return ajax_return($res);
//    }

    public function getEdit()
    {
         $res = $this->goods->getGoodsInfoById(request('id'));
         $flag = 'goods';
         return view('agent.goods.edit_goods',compact('res','flag'));
    }

    //修改商品
    public function postEditSave(){
         //dd($_POST);
         $res = $this->goods->postSave(request());
         return ajax_return($res);
    }


    public function getDel()
    {
        //dd(123);
        $res = $this->goods->changeStatus(request());
        return ajax_return($res);

//        if($res['status']==1){
//            return redirect()->back()->with('message', $res['msg'])->withInput();
//        }else{
//            return redirect()->back()->with('message', $res['msg'])->withInput();
//        }
    }

    //批量删除,小杰作品
    public function postDel()
    {

        $res = $this->goods->delAll(request());
        return ajax_return($res);

    }

}
